#include "SimpleTFGeneralizer.h"



SimpleTFGeneralizer::~SimpleTFGeneralizer(void)
{
	for(list<Observation>::iterator it= dataPoints.begin(); it != dataPoints.end(); it++)
		if (*it)
			delete[] (*it); 

	dataPoints.clear(); 
}


void SimpleTFGeneralizer::learn(const Transition* t) 
{
	Observation cdata = MREAgent::copyObservation(t->start); 
	dataPoints.push_back(cdata); 


	for(int i=0; i< obs_dim; i++)
	{
		double target = t->end[i]; 
		if (LEARN_DIFFERENCES)
			target -= t->start[i]; 
		learners[t->action][i]->addPoint(dataPoints.back(),target); 
	}
}

double SimpleTFGeneralizer::getConfidence(Observation st, Action a)
{
	double min = 1; 
	for(int i=0; i < obs_dim; i++)
	{
		double tmp = learners[a][i]->getConfidence(st); 
		if (tmp < min)
			min = tmp; 
	}

	return min; 
}


Observation SimpleTFGeneralizer::predict(Observation st, Action a)
{
	Observation result = new Observation_type[obs_dim]; 

	for(int i=0; i < obs_dim; i++)
	{
		if (! learners[a][i]->predict(st, result[i]))	//unknown prediction
		{
			delete result; 
			return 0; 
		}

		if (LEARN_DIFFERENCES)
			result[i]+= st[i];  
	}
	return result; 
}

void SimpleTFGeneralizer::batchLearn( list<Transition>& history)
{
/*
	//	printf("size of our list is: %d\n", dataPoints.size()); 
	for(list<Observation>::iterator it= dataPoints.begin(); it!= dataPoints.end(); it++)
		MREAgent::printObservation(*it); 

	exit(0); 
	*/

	for(int i=0; i< action_number; i++)
		for(int j=0; j< obs_dim ; j++)
			learners[i][j]->addPoints(history, i,j);  
}

